package com.wsb.wetchat.config;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * @author wang shaobo
 * @date 2022/6/29  17:17
 */
@Slf4j
@Configuration
public class LogInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler){

        String traceId = request.getHeader("traceId");
        if(traceId == null || "".equals(traceId)){
            traceId = UUID.randomUUID().toString();
        }

        response.setHeader("traceId",traceId);

        MDC.put("traceId",traceId);
        return true;


    }

}
